package com.example.gcmtest1.httpserver;

import java.net.Socket;
import java.util.concurrent.ThreadFactory;

import org.apache.http.impl.DefaultHttpServerConnection;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpService;


public class SingleRequestHandler extends Thread {
	/*
    				DefaultHttpServerConnection serverConnection = new DefaultHttpServerConnection();
    				                
    				serverConnection.bind(socket, new BasicHttpParams());
    				    				
    				//this.httpContext.setAttribute("Socket", socket);
    				BasicHttpContext httpCtx = new BasicHttpContext();
    				httpCtx.setAttribute("Socket", socket);
    				httpCtx.setAttribute("ServerConnection", serverConnection);
    				
    				this.httpService.handleRequest(serverConnection, httpCtx);
    				    				                                        				
    				serverConnection.shutdown();	 
	 */
	
	private Socket socket;
	private DefaultHttpServerConnection serverConnection;
	private BasicHttpContext httpCtx;
	private HttpService httpService;
	
	public SingleRequestHandler(Socket socket, HttpService httpService) throws Exception {
		this.socket = socket;
		this.httpService = httpService;
		
		this.serverConnection = new DefaultHttpServerConnection();
        
		this.serverConnection.bind(socket, new BasicHttpParams());
		    				
		this.httpCtx = new BasicHttpContext();
		this.httpCtx.setAttribute("Socket", socket);
		this.httpCtx.setAttribute("ServerConnection", serverConnection);
		
	}
	
	@Override
	public void run() {
		super.run();
		
		try {
			this.httpService.handleRequest(this.serverConnection, this.httpCtx);
			this.serverConnection.shutdown();			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
    public synchronized void startThread() {
            
    	super.start();
    }

}
